package net.hn.hnms.biz.safety.water.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.hn.hnms.biz.api.mine.RemoteEquipmentService;
import net.hn.hnms.biz.safety.water.domain.GushingrealNowPo;
import net.hn.hnms.biz.safety.water.domain.HolealAbnormalPo;
import net.hn.hnms.biz.safety.water.domain.HolerealNowPo;
import net.hn.hnms.biz.safety.water.mapper.DrainagerealNowMapper;
import net.hn.hnms.biz.safety.water.mapper.GushingrealNowPoMapper;
import net.hn.hnms.biz.safety.water.mapper.HolealAbnormalMapper;
import net.hn.hnms.biz.safety.water.mapper.HolerealNowMapper;
import net.hn.hnms.biz.safety.water.service.StatisticsIndexService;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: StatisticsIndexServiceImpl
 * @Description
 * @Author 王龙
 * @Date 2022-09-24
 * @Version 0.1
 */
@Service
@DS("shfz")
public class StatisticsIndexServiceImpl  implements StatisticsIndexService {
    //涌水实时
    @Resource
    private GushingrealNowPoMapper gushingrealNowPoMapper;

    //排水实时
    @Resource
    private DrainagerealNowMapper drainagerealNowMapper;

    //长观孔实时表
    @Resource
    private HolerealNowMapper holerealNowMapper;

    //长观孔报警
    @Resource
    private HolealAbnormalMapper holealAbnormalMapper;


    //远程调用重大设备
    @Resource
    private RemoteEquipmentService remoteEquipmentService;

    /**
     * 水仓排水涌水监测-涌水量
     * @return
     */
    @Override
    public BigDecimal waterInflow(String mineCode) {
        List<GushingrealNowPo> gushingrealNowPos = gushingrealNowPoMapper.selectList(new LambdaQueryWrapper<GushingrealNowPo>().eq(StringUtils.isNotBlank(mineCode), GushingrealNowPo::getMineCode, mineCode));
        //如果表为空返回零
        if (gushingrealNowPos.isEmpty()) {
            return new BigDecimal(0);
        }
        //倒叙
//        List<GushingrealNowPo> collectVo = gushingrealNowPos.stream().sorted(Comparator.comparing(GushingrealNowPo::getDataTime).reversed()).collect(Collectors.toList());
//        Date dataTime = collectVo.get(0).getDataTime();
//        List<GushingrealNowPo> collect = collectVo.stream().filter(t -> t.getDataTime() == dataTime).collect(Collectors.toList());
//        return collect.stream().sorted(Comparator.comparing(GushingrealNowPo::getPointValue).reversed()).collect(Collectors.toList()).get(0).getPointValue();
        List<GushingrealNowPo> collect = gushingrealNowPos.stream().sorted(Comparator.comparing(GushingrealNowPo::getPointValue).reversed()).collect(Collectors.toList());
        //取最大值
        return collect.get(0).getPointValue();
    }

    /**
     * 水仓排水涌水监测-排水量
     * @return
     */
    @Override
    public BigDecimal waterDischarge(String mineCode) {
//        List<DrainagerealNowPo> drainagerealNowPos = drainagerealNowMapper.selectList(null);
//        //如果表为空返回零
//        if (drainagerealNowPos.isEmpty()) {
//            return new BigDecimal(0);
//        }
//        //倒叙
////        List<DrainagerealNowPo> collectVo = drainagerealNowPos.stream().sorted(Comparator.comparing(DrainagerealNowPo::getDataTime).reversed()).collect(Collectors.toList());
////        //取最新时间
////        Date dataTime = collectVo.get(0).getDataTime();
////        List<DrainagerealNowPo> collect = collectVo.stream().filter(t -> t.getDataTime() == dataTime).collect(Collectors.toList());
////        return collect.stream().sorted(Comparator.comparing(DrainagerealNowPo::getPointValue).reversed()).collect(Collectors.toList()).get(0).getPointValue();
//        List<DrainagerealNowPo> collect = drainagerealNowPos.stream().sorted(Comparator.comparing(DrainagerealNowPo::getPointValue).reversed()).collect(Collectors.toList());
//        //取最大值
//        return collect.get(0).getPointValue();

        //排水量从重大设备水泵房中获取数据
        BigDecimal drainAwayWater = remoteEquipmentService.getDrainAwayWater(mineCode);

        //如果值大于零 则除以60  将立方米每小时改为立方米每分钟
        if (drainAwayWater.compareTo(BigDecimal.ZERO)==1) {
            //保留三位小数，、
            // ROUND_DOWN
            //      接近零的舍入模式。
            //      在丢弃某部分之前始终不增加数字(从不对舍弃部分前面的数字加1，即截短)
            return drainAwayWater.divide(new BigDecimal("60"),3,BigDecimal.ROUND_DOWN);
        }

        return drainAwayWater;
    }

    /**
     * 长观孔总数
     * @return
     */
    @Override
    public Integer count(String mineCode) {
        //查询实时表的个数
        Long aLong = holerealNowMapper.selectCount(new LambdaQueryWrapper<HolerealNowPo>().eq(StringUtils.isNotBlank(mineCode), HolerealNowPo::getMineCode, mineCode));
        return aLong.intValue();
    }

    /**
     * 长观孔报警总数
     * @return
     */
    @Override
    public Integer policeCount(String mineCode) {
        //获取实时表的数据
        List<HolerealNowPo> holerealNowPos = holerealNowMapper.selectList(new LambdaQueryWrapper<HolerealNowPo>().eq(StringUtils.isNotBlank(mineCode), HolerealNowPo::getMineCode, mineCode));
        if (holerealNowPos.isEmpty()) {
            return 0;
        }
        //倒叙
        List<HolerealNowPo> collectVo = holerealNowPos.stream().sorted(Comparator.comparing(HolerealNowPo::getDataTime).reversed()).collect(Collectors.toList());
        HolerealNowPo holerealNowPo = collectVo.get(0);
        if (holerealNowPo != null) {
            //获取实时表最新时间
            Date dataTime = holerealNowPo.getDataTime();
            //封装查询条件，查询当前时间的数据
            LambdaQueryWrapper<HolealAbnormalPo> wrapper = new LambdaQueryWrapper<>();
            //大于等于最新时间
            wrapper.ge(HolealAbnormalPo::getDataTime, dataTime);
            wrapper.eq(StringUtils.isNotBlank(mineCode), HolealAbnormalPo::getMineCode, mineCode);
            //获取数量
            Long aLong = holealAbnormalMapper.selectCount(wrapper);
            return aLong.intValue();
        }
        return 0;
    }
}
